import { defineStore } from 'pinia'
import { ChatMessage } from '~@/api/dashboard/chat'
import { ref } from 'vue'
import { message as Message } from 'ant-design-vue';
import { getHistory } from '~@/api/dashboard/chat'
import { chatlist } from '@/pages/dashboard/analysis/components/history.vue'

export const chatChatStore = defineStore('chat',() => {
    const initialData:ChatMessage[] = [];
    const chatMessage = ref<ChatMessage[]>(initialData)
    let message:any = ref(null)
    const userStore = useUserStore()
    const chatId = ref<Number | null>(null)
    let menuItem1 = ref<chatlist>({
        list: null
      })
    function clearChatMessages() {
        chatMessage.value = [];
    }
    function updateChatMessage(newMessage?:any) {
        if(!newMessage){
            // console.log('参数为空')
            chatMessage.value = []
        }
        chatMessage.value = newMessage
    }
    function getChatMessage() {
        return chatMessage.value
    }
    function getChatId() {
        return chatId.value
    }
    function updateChatId(params:Number | null) {
        return chatId.value = params
    }
    function updatemessage(params:string) {
        return message.value = params
    }
    function getmenuItem1() {
        return menuItem1.value
    }
    // 获取历史列表
    async function queryChat() {
        if (!userStore.userInfo) {
        Message.error('未查到用户，请重新登录！');
        return
        }
        // console.log('userStore.userInfo', userStore.userInfo)
        const { data } = await getHistory({
        id: Number(userStore.userInfo.id),
        pageNum: 1,
        pageSize: 15
        })
        // console.log(data);
        /* if(menuItem1.value.list) {
            menuItem1.value.list = [...menuItem1.value.list, ...data.list];
        } else {
            menuItem1.value.list = data.list
        } */
        menuItem1.value.list = data.list
        // console.log('menuItem', menuItem1.value)
    } 

    // 追加历史列表
    async function addqueryChat() {
        if (!userStore.userInfo) {
            Message.error('未查到用户，请重新登录！');
            return
            }
            // console.log('userStore.userInfo', userStore.userInfo)
            const { data } = await getHistory({
            id: Number(userStore.userInfo.id),
            pageNum: 1,
            pageSize: 15
            })
            // console.log(data);
            if(menuItem1.value.list) {
                menuItem1.value.list = [...menuItem1.value.list, ...data.list];
            } else {
                menuItem1.value.list = data.list
            }
            // console.log('menuItem', menuItem1.value)
    }

    return {
        chatMessage,
        message,
        clearChatMessages,
        updateChatMessage,
        getChatMessage,
        getChatId,
        updateChatId,
        updatemessage,
        queryChat,
        getmenuItem1
    }
})
